package cn.yunhe.service;

import cn.yunhe.dao.IOrder;
import cn.yunhe.entity.Order;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class IOrderService {

    //新增订单
    public int addOrder(Order order){
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        int flag=0;
        try {
            IOrder iOrder=session.getMapper(IOrder.class);
               flag= iOrder.addOrder(order);
              session.commit();
        }catch(Exception e) {
            e.printStackTrace();
        }finally
         {
            session.close();
        }

              return flag;

    }



    //查询所有订单
    public List<Order> getAllOrders(){
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        try {
        IOrder iOrder=session.getMapper(IOrder.class);
           return iOrder.getAllOrders();
        }finally {
            session.close();
        }
    }

    //查询订单记录数
   public int getCount(){
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
          try {
              IOrder iOrder=session.getMapper(IOrder.class);
              return iOrder.getCount();
          }finally {
              session.close();
          }
    }


    //按订单号查询

    public Order getOrderByOrderId(int orderId){
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
       try {
           IOrder iOrder= session.getMapper(IOrder.class);
           return iOrder.getOrderByOrderId(orderId);
       }finally {
           session.close();
       }
    }
   //禁用订单状态
    public boolean disableOrderByOrderId(int state,int orderId){
        boolean flag=false;
            SqlSession session=SqlSessionFactoryUtil.getSqlSession();
            try {
                IOrder iOrder=session.getMapper(IOrder.class);
                flag =iOrder.disableOrderByOrderId(state,orderId);
                session.commit();
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                session.close();
            }
           return flag;
    }



    //按快递类型分组
    public List<Order> getCountByTypeId(){
        SqlSession session= SqlSessionFactoryUtil.getSqlSession();
        try {
            IOrder iOrder=session.getMapper(IOrder.class);
            return iOrder.getCountByType();

        }finally {
            session.close();
        }
    }



}
